package com.dyrj.question.service.impl;

import com.dyrj.question.bean.PRandques;
import com.dyrj.question.bean.PTkgl;
import com.dyrj.question.bean.PZslb;
import com.dyrj.question.bean.PZsnr;
import com.dyrj.question.dao.PRandquesMapper;
import com.dyrj.question.dao.PTkglMapper;
import com.dyrj.question.dao.PZslbMapper;
import com.dyrj.question.dao.PZsnrMapper;
import com.dyrj.question.service.IPZslbService;
import com.dyrj.utils.DateUtil;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 知识类别管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Service
@Log4j2
public class PZslbServiceImpl implements IPZslbService {

    @Resource
    private PZslbMapper mapper;
    @Resource
    private PZsnrMapper pZsnrMapper;
    @Resource
    private PTkglMapper pTkglMapper;
    @Resource
    private UploadProperties uploadProperties;

    @Override
    public PageResultMsg table(PZslb record) {
        PageInfo<PZslb> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(PZslb record) {
        String msg = "";
        try {
            record.setCjsj(DateUtil.parse(record.getCjsjtext(),"yyyy-MM-dd HH:mm:ss"));
            record.setBjsj(DateUtil.getDate());
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public PZslb info(String id) {
        PZslb ylMmdagl = mapper.selectByPrimaryKey(Integer.parseInt(id));
        return ylMmdagl;
    }

    public ResultMsg update(PZslb record) {
        String msg = "";
        try {
            record.setCjsj(DateUtil.parse(record.getCjsjtext(),"yyyy-MM-dd HH:mm:ss"));
            record.setBjsj(DateUtil.getDate());
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

    public List getZslbList(String sjlbdm){

        PZslb pZslb1 = new PZslb();
        List<HashMap> retList = new ArrayList();
        pZslb1.setSjlbdm(sjlbdm);
        List<PZslb> pZslbList =  mapper.selectList(pZslb1);
        for(int i=pZslbList.size()-1;i>=0;i--){
            HashMap retMap = new HashMap();
            PZslb pZslb = pZslbList.get(i);
            PZsnr pZsnr = new PZsnr();
            pZsnr.setLbdm(pZslb.getLbdm());
            List<PZsnr> list= pZsnrMapper.selectList(pZsnr);
            retMap.put("lbmc",pZslb.getLbmc());
            retMap.put("list",list);
            retList.add(retMap);
        }
        return retList;
    }

}
